package com.zzf;

import com.alibaba.fastjson.JSONObject;
import com.magic.web.common.controller.DateUtils;
import com.magic.web.common.controller.HttpUtils;
import com.magic.web.common.util.SecurityUtils;
import com.sun.org.apache.xerces.internal.xs.datatypes.ObjectList;
import lombok.ToString;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @description
 * @author wm
 * @date 2021/5/20
 */

public class testDemo {

    private static SimpleDateFormat YMD = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat MD = new SimpleDateFormat("yyyy-M-d");

    private static SimpleDateFormat YMDH = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat YM = new SimpleDateFormat("yyyy-M");
    private static SimpleDateFormat YMM = new SimpleDateFormat("yyyy-MM");
    private static String[] a = {"1", "1", "1", "1", "1", "1", "1", "1", "1", "1"};
    private static final Logger logger = LoggerFactory.getLogger(testDemo.class);

    @Test
    public void test01() {
        Calendar calendar = Calendar.getInstance();
        Date date = new Date();
        calendar.setTime(date);
        int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        System.out.println(week);
    }

    @Test
    public void test02() {
        //String b = "";
        for (int i = 0; i < 10; i++) {
            a[i] = String.valueOf(i);
            System.out.println(a[i]);
        }
        System.out.println(a[6]);
    }

    @Test
    public void test03() {
        Date date = new Date();
        String format = YMM.format(date);
        System.out.println(format);
    }

    @Test
    public void test04() {
        double pz_total_number = changedouble(Double.parseDouble("571.09"));
        double pz_zl_money_1_number = changedouble(Double.parseDouble("571.10"));
        double pz_zl_money_2_number = changedouble(Double.parseDouble("0.00"));
        double pz_zl_money_3_number = changedouble(Double.parseDouble("0.00"));
        double pz_zl_money_4_number = changedouble(Double.parseDouble("0.00"));
        if (changedouble(pz_zl_money_1_number + pz_zl_money_2_number + pz_zl_money_3_number + pz_zl_money_4_number) > pz_total_number) {
            System.out.println("该保单的四个列支总额大于配置总额,无法修改数据!");
        } else {
            System.out.println("成功");
        }
    }

    @Test
    public void test05() {
        String a = "0.00";
        BigDecimal bigDecimal = new BigDecimal(a);
        BigDecimal bigDecimal1 = new BigDecimal(1);
        BigDecimal bigDecimal2 = new BigDecimal(2);
        BigDecimal add = bigDecimal.add(bigDecimal1).add(bigDecimal2);
        System.out.println(add);

    }

    @Test
    public void test06() throws ParseException {
        String date = "2021-05-31";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(sdf.parse(date));
        c.set(Calendar.DAY_OF_MONTH, 1);
        String startdate = sdf.format(c.getTime());
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        String enddate = sdf.format(c.getTime());
        System.out.println(startdate);
        System.out.println(enddate);
    }

    @Test
    public void test07() {
        String clauses_9931082 = "9座含及以下非营运客车含家用车，保险有效期内，发生自然灾害前或防止意外发生，" +
                "被保险车辆可免费享受保险人提供的指定地点车辆安全停靠服务7次，每次停放时间不超过2小时，" +
                " 具体使用方法请关注绑定“厦门人保财险”公众号。";
        String clauses9931082 = "";
        if (StringUtils.isNotEmpty(clauses_9931082)) {
            char[] strChars = clauses_9931082.toCharArray();
            List<String> arrayList = new ArrayList<>();
            for (int i = 0; i < strChars.length; i++) {
                try {
                    int n = Integer.parseInt(strChars[i] + "");
                    arrayList.add(String.valueOf(n));
                    System.out.print(n + "  ");
                } catch (NumberFormatException e) {
                    continue;
                }
            }
            arrayList.remove(0);
            arrayList.remove(arrayList.size() - 1);
            for (String s : arrayList) {
                clauses9931082 += s;
            }
            System.out.println(clauses9931082);
        }
        System.out.println(clauses9931082);
    }

    @Test
    public void test08() {
        String timestamp = getCurrTimestamp();
        String passToken = null;
        String app_code = "carinsmng";
        String password = "iu5s6VRC";
        String md5Str = SecurityUtils.getMD5String(timestamp + password + app_code);
        String pass_token = md5Str.toUpperCase();
        System.out.println(pass_token);
        logger.info("wx_timestamp=" + timestamp);
        logger.info("wx_pass_token=" + pass_token);
        String url = "http://42.1.39.154/xminfogrid/wxwork/v1/pub/message?app_code=" + app_code + "&timestamp=" + timestamp + "&pass_token=" + passToken;
        //处理消息格式
        int agentid = 1000022;
        JSONObject json = new JSONObject();
        json.put("touser", "bpsoft_zhangzf");
        json.put("agentid", agentid);
        json.put("msgtype", "textcard");
        String title = "测试发送信息";
        String type = "部门负责人审批";
        String task_id = "111111111111111";
        String xqUrl = "https://testwx.gwjlb.com/xmweb/cxzh/dist/index.html?user_id=" + "bpsoft_zhangzf" + "&from=msg&task_id=" + task_id + "&node_no=Manager";
        Map<String, Object> sendmessage = new HashMap<String, Object>();
        //测试title参数
        sendmessage.put("title", "车险超权限审批通知");

        String description = "<div class=\"highlight\">" + title + "</div>"
                + "<div class=\"gray\">报批号：" + task_id + "</div>"
                + "<div class=\"gray\">节点名称：" + type + "</div>"
                + "<div class=\"gray\">创建时间：" + YMDH.format(new Date()) + "</div> ";
        sendmessage.put("description", description);
        sendmessage.put("url", xqUrl);
        sendmessage.put("btntxt", "进入审批");
        json.put("textcard", sendmessage);
        json.put("enable_id_trans", 0);
        json.put("enable_duplicate_check", 0);
        json.put("duplicate_check_interval", 1800);
        //发送消息json字符串
        String jsonObject = json.toString();
        logger.info("url-->jsonObject:" + jsonObject);
        logger.info("url-->" + url);
        String body = "";
        try {
            body = HttpUtils.sendMessageByPost(url, jsonObject);
            //错误信息处理
            errmsgHandle(body);
            System.out.println(body);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test09() {
        BigDecimal bigDecimal = new BigDecimal(0).setScale(2);
        if (!bigDecimal.equals(new BigDecimal(0).setScale(2))) {
            System.out.println("1");
        } else {
            System.out.println("2");
        }
        if (!(bigDecimal.compareTo(new BigDecimal(0)) == 0)) {
            System.out.println("3");
        } else {
            System.out.println("4");
        }
    }

    @Test
    public void test10() {
        String[] array = {"1", "2", "3", "5"};
        System.out.println(array);
        int size = 11;
        System.out.println(Math.ceil(size * 0.66));
        double a = 21.12;
        System.out.println(Math.ceil(a));
    }

    @Test
    public void test11() {
        String a = "2";
        //System.out.println(a.length());
        char[] charArray = a.toCharArray();
        char c = charArray[a.length() - 1];
        for (int i = 0; i < charArray.length - 1; i++) {
            System.out.println(charArray[i]);
        }
        int b = Integer.parseInt(String.valueOf(c));
        //System.out.println(c);
        System.out.println(b);
        if (b >= 0 && b < 5) {
            System.out.println("1");
        } else if (b >= 5 && b < 10) {
            System.out.println("2");
        } else {
            System.out.println("3");
        }
    }

    @Test
    public void test12() {
        String a = "南山街道办事处";
        if (a.contains("支公司")) {
            a = a.substring(0, a.length() - 3);
        }
        System.out.println(a);
    }

    @Test
    public void test13() {
        BigDecimal bigDecimal = new BigDecimal(100);
        BigDecimal bigDecimal1 = new BigDecimal(-100);
        if (bigDecimal.compareTo(bigDecimal1) == 1) {
            System.out.println("dayu");
        } else {
            System.out.println("xiaoyu");
        }
        String s = RandomStringUtils.randomNumeric(1);
        System.out.println(s);
    }

    @Test
    public void test14() {
        List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < 10; i++) {
            HashMap<String, Object> map = new HashMap<>();
            String s = RandomStringUtils.randomNumeric(1);
            map.put("id",s);
            list.add(map);
        }
        System.out.println("排序前------------------------------------------------");
        for (Map<String, Object> map : list) {
            String id = (String) map.get("id");
            System.out.println(id);
        }
        SortList1<Map<String,Object>> sortList = new SortList1<Map<String,Object>>();
        sortList.Sort(list,"id","desc");
        System.out.println("排序后------------------------------------------------");
        for (Map<String, Object> map : list) {
            String id = (String) map.get("id");
            System.out.println(id);
        }
    }

    @Test
    public void test15 (){
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        list.add("5");
        list.add("6");
        list.add("7");
        for (int i = list.size()-5; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        for (int i = 0; i < 3; i++) {
            System.out.println(list.get(i));
        }
    }

    @Test
    public void test16 (){
        SimpleDateFormat  sdf = new  SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date a = new Date(1623999223000l);
        System.out.println(sdf.format(a));
        System.out.println(sdf.format(new Date()));
    }

    public double changedouble(double a) {
        //四舍五入保留两位小数
        return (double) Math.round(a * 100) / 100;
    }

    public boolean checkMoney(String money) {
        //把string转为double
        BigDecimal bd = new BigDecimal(money);
        Double data = bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        if (data > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取时间戳
     * @return
     */
    public static String getCurrTimestamp() {
        return DateUtils.format(new Date(), "yyyyMMddHHmmssSSS");
    }

    /**
     * 错误信息处理
     * @param body
     * @throws Exception
     */
    private static void errmsgHandle(String body) throws Exception {
        //解析结果
        JSONObject jsonObject = JSONObject.parseObject(body);
        if (null != jsonObject) {
            //错误消息处理
            if (jsonObject.getInteger("errcode") != null && 0 != jsonObject.getInteger("errcode")) {
                int errCode = jsonObject.getInteger("errcode");
                String errMsg = jsonObject.getString("errmsg");
                logger.info("errcode=" + errCode);
                logger.info("errMsg=" + errMsg);
                throw new Exception("error code:" + errCode + ", error message:" + errMsg);
            }
        }
    }

    /**
     * 适用于实体类
     * @param <E>
     */
    public class SortList<E> {
        public void Sort(List<E> list, final String method, final String sort) {
            list.sort((E a, E b) -> {
                int ret = 0;
                try {
                    Method m1 = ((E) a).getClass().getMethod(method, (Class<?>) null);
                    Method m2 = ((E) b).getClass().getMethod(method, (Class<?>) null);
                    if ("desc".equals(sort)) {
                        //倒序
                        ret = m2.invoke(((E) b), (Object) null).toString().compareTo(m1.invoke(((E) a), (Object) null).toString());
                    } else {
                        //正序
                        ret = m1.invoke(((E) a), (Object) null).toString().compareTo(m2.invoke(((E) b), (Object) null).toString());
                    }
                } catch (Exception ne) {
                    System.out.println(ne);
                }
                return ret;
            });
        }
    }

    /**
     * 适用于map
     * @param <E>
     */
    public class SortList1<E> {
        public void Sort(List<Map<String, Object>> list, final String method, final String sort) {
            list.sort((Map<String, Object> a, Map<String, Object> b) -> {
                int ret = 0;
                try {
                    String m1 = (String) ((Map<String, Object>) a).get(method);
                    String m2 = (String) ((Map<String, Object>) b).get(method);
                    if ("desc".equals(sort)) {
                        //倒序
                        ret = m2.compareTo(m1);
                    } else {
                        //正序
                        ret = m1.compareTo(m2);
                    }
                } catch (Exception ne) {
                    System.out.println(ne);
                }
                return ret;
            });
        }
    }

}
